package com.sundablog.service.impl.backend.system.upms.role;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminRoleMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.pojo.AdminRole;
import com.sundablog.pojo.AdminRoleExample;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminRolePermissionExample.Criteria;
import com.sundablog.result.LayUiResult;
import com.sundablog.service.backend.system.upms.role.RoleService;

import cn.hutool.core.util.StrUtil;

/**
 * 角色接口实现类
 * @ClassName:  RoleServiceImpl   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:02:07   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private AdminRoleMapper adminRoleMapper;
	
	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;
	
	/**
	 * 添加角色信息
	 * Title: addRole   
	 * Description:    
	 * @param adminRole
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#addRole(com.sundablog.pojo.AdminRole)
	 */
	@Override
	public int addRole(AdminRole adminRole) {
		
		int i = adminRoleMapper.insertSelective(adminRole);
		return i;
	}

	/**
	 * 更新角色信息
	 * Title: updataRole   
	 * Description:    
	 * @param adminRole
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#updataRole(com.sundablog.pojo.AdminRole)
	 */
	@Override
	public int updataRole(AdminRole adminRole) {
		int i = adminRoleMapper.updateByPrimaryKeySelective(adminRole);
		return i;
	}

	/**
	 * 删除角色信息
	 * Title: delectRoleByRoleId   
	 * Description:    
	 * @param roleId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#delectRoleByRoleId(int)
	 */
	@Override
	public int delectRoleByRoleId(int roleId) {
		int i = adminRoleMapper.deleteByPrimaryKey(roleId);
		if (i > 0) {
			AdminRolePermissionExample example = new AdminRolePermissionExample();
			Criteria criteria = example.createCriteria();
			criteria.andRoleIdEqualTo(roleId);
			int j = adminRolePermissionMapper.deleteByExample(example);
			if (j  > 0) {
				return j;
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	/**
	 * 根据角色id查询角色信息
	 * Title: selectRoleByRoleId   
	 * Description:    
	 * @param roleId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRoleByRoleId(int)
	 */
	@Override
	public AdminRole selectRoleByRoleId(int roleId) {
		AdminRole adminRole = adminRoleMapper.selectByPrimaryKey(roleId);
		return adminRole;
	}

	/**
	 * 分页查询
	 * Title: findPagingQuery   
	 * Description:    
	 * @param page
	 * @param limit
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#findPagingQuery(int, int)
	 */
	@Override
	public LayUiResult findPagingQuery(int page, int limit) {
		PageHelper.startPage(page, limit);
		AdminRoleExample example = new AdminRoleExample();
		List<AdminRole> list = adminRoleMapper.selectByExample(example);
		PageInfo<AdminRole> pageInfo = new PageInfo<>(list);
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(pageInfo.getTotal());
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 根据角色id查询角色权限关联表
	 * Title: selectRolePermissionByRoleId   
	 * Description:    
	 * @param roleId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRolePermissionByRoleId(int)
	 */
	@Override
	public List<AdminRolePermission> selectRolePermissionByRoleId(int roleId) {
		AdminRolePermissionExample example = new AdminRolePermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<AdminRolePermission> list = adminRolePermissionMapper.selectByExample(example);
		return list;
	}

	/**
	 * 根据角色ID 向权限关联表中添加权限id
	 * Title: addRolePermission   
	 * Description:    
	 * @param roleId
	 * @param permissionId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#addRolePermission(int, java.lang.String)
	 */
	@Override
	public int addRolePermission(int roleId, String permissionId) {
		//删除角色权限关联表中是数据
		AdminRolePermissionExample example = new AdminRolePermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		int i = adminRolePermissionMapper.deleteByExample(example);
		
		//添加
		List<String> list = StrUtil.split(permissionId, ',');
		try {
			for (String string : list) {
				AdminRolePermission adminRolePermission = new AdminRolePermission();
				adminRolePermission.setRoleId(roleId);
				adminRolePermission.setPermissionId(Integer.valueOf(string));
				adminRolePermissionMapper.insert(adminRolePermission);
			}
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 查询全部角色
	 * Title: selectRole   
	 * Description:    
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.role.RoleService#selectRole()
	 */
	@Override
	public List<AdminRole> selectRole() {
		AdminRoleExample example = new AdminRoleExample();
		List<AdminRole> list = adminRoleMapper.selectByExample(example);
		return list;
	}

}
